package com.hongshan.wallet.core.wallet

import android.arch.lifecycle.ViewModelProviders
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.allenliu.versionchecklib.v2.AllenVersionChecker
import com.allenliu.versionchecklib.v2.builder.NotificationBuilder
import com.allenliu.versionchecklib.v2.builder.UIData
import com.hongshan.wallet.BR
import com.hongshan.wallet.BuildConfig
import com.hongshan.wallet.Constant
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.R
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.http.HttpClient
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.toast
import com.hongshan.wallet.tryRun
import com.hongshan.wallet.util.MyActivityManager
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.appbar.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch


@Route(path = RouterIndex.SETTING)
class SettingActivity : MyActivity<ViewDataBinding, MyViewModel>() {

    override fun initVariableId() = BR.settingVm

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_setting

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tvTitle.text = "设置"
        tvVersionNumber.setText(BuildConfig.VERSION_NAME)
        tvVersion.setOnClickListener {
            showLoading()
            GlobalScope.launch(Dispatchers.Main) {
                val async = GlobalScope.async(Dispatchers.IO) {
                    tryRun {
                        HttpClient.httpApi.update(BuildConfig.VERSION_NAME).execute()
                    }
                }
                val body = async.await()?.body()
                showLoadSuccess()
                if (body?.code == 0) {
                    if (body.data.is_update) {
                        val uiData = UIData
                            .create()
                            .setDownloadUrl(body.data.apk_url)
                            .setTitle("有新的应用程序版本")
                            .setContent(body.data.new_version)
                        val instance = AllenVersionChecker.getInstance().downloadOnly(uiData)
                        if (body.data.is_force_update) {
                            instance.setForceUpdateListener {
                                MyActivityManager.exitApp()
                            }
                        }
                        instance.setForceRedownload(true)
                            .setNotificationBuilder(
                                NotificationBuilder.create()
                                    .setRingtone(true)
                                    .setIcon(R.mipmap.ic_launcher)
                                    .setTicker(resources.getString(R.string.appName))
                                    .setContentTitle("检查更新")
                                    .setContentText("有新版本可用")
                            )

                            .setDownloadAPKPath(Constant.FilePath.DATA_CACHE_FOLDER)
                            .executeMission(this@SettingActivity)
                    } else{
                        toast("已是最新版本")
                    }
                }
            }
        }
        tvExit.setOnClickListener {
            AlertDialog.Builder(this)
                .setMessage("确定退出登录?")
                .setPositiveButton("确定") { dialog, _ ->
                    MyApplication.user?.clean()
                    dialog.dismiss()
                    finish()
                    go(RouterIndex.LOGIN)
                }
                .setNegativeButton("取消"){dialog, _ ->
                    dialog.dismiss()
                }
                .create()
                .show()
        }
    }

    override fun initViewModel() = ViewModelProviders.of(this)[SettingViewModel::class.java]
}
